package com.cleaningcloud.erp.controller.mini;

import com.cleaningcloud.common.core.domain.R;
import com.cleaningcloud.common.core.utils.StringUtils;
import com.cleaningcloud.common.core.web.controller.BaseController;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.security.annotation.InnerAuth;
import com.cleaningcloud.common.security.utils.DictUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.RemoteCustomerService;
import com.cleaningcloud.erp.api.domain.ErpCustomer;
import com.cleaningcloud.erp.api.model.LoginCustomer;
import com.cleaningcloud.erp.domain.mini.UserInfo;
import com.cleaningcloud.erp.service.IErpCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static com.cleaningcloud.common.core.web.domain.AjaxResult.error;
import static com.cleaningcloud.common.core.web.domain.AjaxResult.success;

/**
 * packageName com.cleaningcloud.erp.controller.mini
 *
 * @author 你的名字
 * @version JDK 8
 * @className MallLoginController
 * @date 2025-04-01
 * @description 三才环科商城(小程序)登录接口
 */
@RestController
@RequestMapping("/mall")
public class MallLoginController extends BaseController {

    @Autowired
    private IErpCustomerService erpCustomerService;

    @Autowired
    private RemoteCustomerService remoteCustomerService;


    /**
     * @Author Shaw
     * @Description 用户登录
     * @date: 2025-04-01 16:38
     * @param: //TODO
     */
    @InnerAuth
    @GetMapping("/info/{userName}")
    public R<LoginCustomer> info(@PathVariable("userName") String userName){
        ErpCustomer erpCustomer = erpCustomerService.getCustomerInfoByUserName(userName);
        if (StringUtils.isNull(erpCustomer)) {
            return R.fail("该账号不存在，请先注册！");
        }
        LoginCustomer loginCustomer = new LoginCustomer();
        Long customerId = erpCustomer.getCustomerId();
        String customerName = erpCustomer.getCustomerName();
        loginCustomer.setCustomerId(customerId);
        loginCustomer.setCustomerName(customerName);
        loginCustomer.setErpCustomer(erpCustomer);
        return R.ok(loginCustomer);
    }

    /**
     * @Author Shaw
     * @Description 注册客户信息
     * @date: 2025-04-01 16:11
     * @param: //TODO
     */
    @PostMapping("/register")
    public R<Boolean> register(@RequestBody ErpCustomer erpCustomer) {
        String userName = erpCustomer.getUserName();
        if (!erpCustomerService.checkUserNameUnique(erpCustomer)) {
            return R.fail("注册失败，手机号'" + userName + "'已存在");
        }
        return R.ok(erpCustomerService.registerCustomer(erpCustomer));
    }

    /**
     * @Author Shaw
     * @Description 获取用户信息
     * @date: 2025-04-01 17:19
     * @param: //TODO
     */
    @InnerAuth
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        Map<String, String> customerType = DictUtils.getDictDataMap("erp_customer_type");
        Map<String, String> customerLevel = DictUtils.getDictDataMap("erp_customer_level");
        Map<String, String> customerNature = DictUtils.getDictDataMap("erp_customer_nature");
        Map<String, String> customerIndustry = DictUtils.getDictDataMap("erp_customer_industry");
        UserInfo userInfo = new UserInfo();
        // 获取登录用户
        LoginCustomer loginCustomer = SecurityUtils.getLoginCustomer();
        ErpCustomer erpCustomer = erpCustomerService.selectErpCustomerByCustomerId(loginCustomer.getCustomerId());
        if(null != erpCustomer){
            userInfo.setErpCustomer(erpCustomer);
            userInfo.setCustomerType(customerType.get(erpCustomer.getCustomerType().toString()));
            userInfo.setCustomerLevel(customerLevel.get(erpCustomer.getCustomerType().toString()));
            userInfo.setCustomerNature(customerNature.get(erpCustomer.getCustomerType().toString()));
            userInfo.setCustomerIndustry(customerIndustry.get(erpCustomer.getCustomerType().toString()));
            userInfo.setUserId(erpCustomer.getCustomerId());
            userInfo.setUserName(erpCustomer.getCompanyName());
        }else{
            return error("获取用户信息失败");
        }
        return success(userInfo);
    }
}